<!-- 首页 -->
<template>
  <JeepayBackground :bgColorStyle="{}">
    <!-- 导航条 -->
    <JeepayCustomNavbar title="首页" textColor="#fff"
      bgDefaultColor="linear-gradient(270deg, rgba(72, 192, 255, 1) 0%, rgba(51, 157, 255, 1) 100%)" />

    <!-- 统计 or 快捷扫码 -->
    <Stats ref="statsRef" />

    <!-- 导航栅格 -->
    <JeepayNavigation :navList="navList" type="grid" />
    <blcok v-for="v in vdata.adList" :key="v.adverId">
      <JeepayBanner :list="v.appContent" :interval='v.changeTime' v-if="v.appPlaceType == 2" />
      <JeepayAdCard :list='v.appContent' v-if="v.appPlaceType == 1" />
    </blcok>
    <view style="height:20rpx;" v-if="vdata.adList.length"></view>
    <!-- 公告消息前几条 , 自定义显示字段。  -->
    <JeepayListview tableTitle="最新公告" :dataList="vdata.noticeList" @click="listviewClickFunc"
      :fields="{ title: 'title', subtitle: 'createdAt' }" />

  </JeepayBackground>
</template>

<script setup>
import {
  reactive,
  ref,
  onMounted,
  nextTick
} from 'vue'
import { onShareAppMessage,onShareTimeline } from '@dcloudio/uni-app'
import {
  reqLoad,
  API_URL_SYS_ARTICLES,
  $indexStatistics,
  $getSiteInfos
} from '@/http/apiManager.js'
import go from '@/commons/utils/go.js'
import Stats from './components/Stats.vue'
import registerPush from "@/commons/utils/pushmsg/registerPush.js"
import pushMsgManage from "@/commons/utils/pushmsg/pushMsgManage.js"

import {
  onPullDownRefresh,
  onLoad
} from '@dcloudio/uni-app'
import storageManage from '@/commons/utils/storageManage.js'
import {
  $adList
} from '@/http/apiManager.js'
onLoad((options) => {
 //注册 push连接逻辑
  registerPush()
  // 添加语音播报的消息推送
  pushMsgManage.addPushMsgEventListener()
})

// 导航列表
const navList = [{
  title: '通道申请',
  icon: '/static/indexImg/icon-passage.svg',
  pageUrl: 'PAGES_APPLYMENT',
  entId: 'ENT_MCH_APPLYMENT_LIST'
},
{
  title: '我的门店',
  icon: '/static/indexImg/icon-store.svg',
  pageUrl: 'PAGES_STORE',
  entId: 'ENT_MCH_STORE'
},
{
  title: '我的设备',
  icon: '/static/indexImg/icon-calc.svg',
  pageUrl: 'PAGES_DEVICE_MAIN',
  entId: 'ENT_DEVICE'
},
{
  title: '员工管理',
  icon: '/static/indexImg/icon-staff.svg',
  pageUrl: 'PAGES_USER',
  entId: 'ENT_UR_USER_LIST'
},
{
  title: '统计报表',
  icon: '/static/indexImg/icon-pro.svg',
  pageUrl: 'PAGES_STAT',
  entId: 'ENT_ORDER_STATISTIC'
},
{
  title: '商户应用',
  icon: '/static/indexImg/icon-app.svg',
  pageUrl: 'PAGES_APP',
  entId: 'ENT_MCH_APP_LIST'
},
{
  title: '会员中心',
  icon: '/static/indexImg/icon-member.svg',
  pageUrl: 'PAGES_MEMBER_CENTER',
  entId: 'ENT_MCH_MEMBER'
},
{
  title: '广告管理',
  icon: '/static/indexImg/icon-ad.svg',
  pageUrl: 'PAGES_AD_LIST',
  entId: 'ENT_ADVERT_CONTROL'
},
]
// 如果不是超管 删除 刷脸广告菜单
if (storageManage.userInfo().userType != 1) {
  const index = navList.findIndex(v => v.entId == 'ENT_ADVERT_CONTROL')
  if (index != -1) {
    navList.splice(index, 1)
  }
}
const statsRef = ref()

const vdata = reactive({
  noticeList: [], // 公告列表
  adList: [],
  shareImgUrl:'' //分享图片
})

// 公告的点击事件。
const listviewClickFunc = (isClickMore, record) => {
  if (isClickMore) {
    return go.to('PAGES_NOTICE_LIST')
  }

  return go.to('PAGES_NOTICE_DETAIL', {
    id: record.articleId
  })
}

// 刷新数据 async 异步函数， 每个查询需要返回promise对象，并标识await
async function refData () {
  // 查询公告列表
  await reqLoad.list(API_URL_SYS_ARTICLES, {
    pageSize: 5,
    articleType: 1
  }).then(({
    bizData
  }) => {
    vdata.noticeList = bizData.records
  })

  //   调用子组件方法 , 避免组件没有加载完成。
  nextTick(() => statsRef.value.refData())

  // 停止刷新
  uni.stopPullDownRefresh()
}
// 获取 统计数据
const getIndexStat = (time) => {
  $indexStatistics(time).then(({
    bizData
  }) => {
    vdata.statInfo = bizData.dayCount
  })
}

// 请求首页广告
$adList({
  appPlace: 2
}).then(({
  bizData
}) => {
  if (!bizData) return false
  bizData.forEach(v => {
    v.appContent = JSON.parse(v.appContent)
  })
  vdata.adList = bizData
})
// 启动加载
onMounted(() => {
  refData()
})
// 获取配置信息
$getSiteInfos().then(({ bizData }) => {
 vdata.shareImgUrl = bizData.shareImgUrl
})
// 微信分享
onShareAppMessage((res) => {
    if(res.from == 'menu') return {
    title: uni.$appName,
    path: '/pages/index/index',
    imageUrl: vdata.shareImgUrl
  }
})
// 分享到朋友圈
onShareTimeline(() => {
    return {
    title: uni.$appName,
    imageUrl: vdata.shareImgUrl
  }
})
// 下拉刷新
onPullDownRefresh(() => {
  refData()
})
</script>

<style lang="scss" scoped></style>
